var postId,belongToId;
var userId;//发布评论的用户id
mui.plusReady(function() {
	userId = plus.navigator.getCookie( "id");
});
//添加cdetailId自定义事件监听
window.addEventListener('cdetailId',function(event){
	// 获得事件参数
	postId = event.detail.postId;
	belongToId = event.detail.belongToId;
	getTopicDetail();
	getTopicComment();
	checkZan();
});
// 获取帖子详情
function getTopicDetail(){
	$.ajax({
		url:backend_url + "/po/gp.action",
		data:{
			"id":postId
		},
		type:"POST",
		dataType:"json",
		success:function(data){
			likenumInt = data.b.likenumInt;
			$("#content_area").empty();
			var div = `<div class="content-area" id=`+data.b.id+` userId=`+data.b.userId+`>
						<h3 class="issuetitle">`+data.b.title+`</h3>
						<p class="issueType mui-pull-right">
							<span class="mui-icon iconfont icon-dianzan">`+data.b.likenumInt+`</span>
							<span class="mui-icon iconfont icon-shijian">`+data.b.issueTime+`</span>
						</p>
						<p class="content">`+data.b.content+`</p>`;
						// <span class="mui-icon iconfont icon-redu">`+data.b.heat+`</span>
			$("#content_area").append(div);
		},
		error:function(er){
			// console.log(er);
			// alert("呃。。网络似乎除了什么问题，请检查网络连接或重试！");
			//window.location.reload();
		}
	});
}
// 获取帖子评论
function getTopicComment(){
	$.ajax({
		url:backend_url + "/co/gcl.action",
		data:{
			"postId":postId
		},
		type:"POST",
		dataType:"json",
		success:function(data){
			var comment_list = data.b;
			// console.log(comment_list);
			$("#comment_content").empty();
			for(var i =0 ; i< comment_list.length;i++){
				var div =  `<div class="mui-card" issueId=`+comment_list[i].issueId+`>
								<p style="margin:10px 20px;">`+comment_list[i].commentContent+`</p>
								<span class="mui-pull-right" style="margin:5px 20px;color:#09a267;">`+comment_list[i].reserved1+`</span>
							</div>`;
				$("#comment_content").prepend(div);
			}
		},
		error:function(er){
			// console.log(er);
			// alert("呃。。网络似乎除了什么问题，请检查网络连接或重试！");
			//window.location.reload();
		}
	});
}
/// 判断用户是否已点赞
function checkZan(){
	$.ajax({
		url:backend_url + "/li/gplu.action",
		data:{
			"postId":postId,
			"userId":userId 
		},
		type:"POST",
		dataType:"json",
		success:function(data){
			if(data.a){
				zan_flag = 1;
			}else{
				zan_flag = 0;
				$("#zan").css("color","red");
			}
		},
		error:function(er){
			// console.log(er);
			// alert("呃。。网络似乎除了什么问题，请检查网络连接或重试！");
			//window.location.reload();
		}
	});
}
// 点赞
var likenumInt;
var zan_flag = 1;//0已点赞；1未点赞
$("#zan").on("tap",function(){
	if(typeof(userId) == "string"){
		if(!zan_flag){
			mui.toast("你已经点过赞了！");
		}else{
			$.ajax({
				url:backend_url + "/po/al.action",
				data:{
					"id":postId,
					"userId":userId,
					"likenumInt":likenumInt
				},
				type:"POST",
				dataType:"json",
				success:function(data){
					if(data.a){
						$("#zan").css("color","red");
					}else{
						
					}
				},
				error:function(er){
					// console.log(er);
					// alert("呃。。网络似乎除了什么问题，请检查网络连接或重试！");
					//window.location.reload();
				}
			});
		}
	}else{
		var iflogin = confirm("暂未登录，是否前去登录？");
		if(iflogin){
			plus.webview.open('./before-login.html', 'new', {}, 'slide-in-right', 200);
		}else{
			return false;
		}
	}
});
// 发起评论
$("#c_send").on("tap",function(){
	if(typeof(userId) == "string"){
		var c_content = $("#c_content").val();
		if(c_content == ""){
			mui.toast("评论内容不能为空哦！");
			$("#c_content").css("border","1px solid red");
		}else if(postId == undefined ){
			mui.toast("找不到帖子资源了555~");
			return false;
		}else{
			$("#c_content").css("border","none");
			$.ajax({
				url:backend_url + "/co/ac.action",
				data:{
					"commentContent":c_content,
					"userId":userId,//发布评论的用户id
					"postId":postId,//帖子id
					"belongTo":belongToId//被评论的用户id
				},
				type:"POST",
				dataType:"json",
				success:function(data){
					if(data.a){
						$("#c_content").val("");
						mui.toast("评论成功");
						getTopicComment();
					}
				},
				error:function(er){
					// console.log(er);
					// alert("呃。。网络似乎除了什么问题，请检查网络连接或重试！");
					//window.location.reload();
				}
			});
		}
	}else{
		var iflogin = confirm("暂未登录，是否前去登录？");
		if(iflogin){
			plus.webview.open('./before-login.html', 'new', {}, 'slide-in-right', 200);
		}else{
			return false;
		}
	}
});

